<script setup>

import {doLogout, getLoginUser} from "@/api/httpApi/userApi.js";
import {userStore} from "@/stores/userStore.js";
import {successCode} from "@/api/httpApi/base/http.js";
import {onMounted, ref} from "vue";
import MyTag from "@/components/constant/myTag.vue";
import {success} from "@/api/myMessage.js";
import {adminUrl, portalUrl} from "@/api/const.js";

onMounted(() => {
  getUserInfo();
})
let userLogin = ref({})

function getUserInfo() {
  if (userStore().loginUser === null) {
    getLoginUser().then((res) => {
      if (successCode === res.code) {
        userStore().setLoginUser(res.data)
      }
    })
  }
  userLogin.value = userStore().loginUser;
}

function logout() {
  doLogout().then(res => {
    if (res.code === successCode) {
      success(res.message);
      userStore().logout();
      location.href = portalUrl + "/login?redirect=" + adminUrl;
    }
  })
}

let powerTableOption = [
  {type: "info", data: userStore().powerTable[0].label, msg: userStore().powerTable[0].label},
  {type: "warring", data: userStore().powerTable[1].label, msg: userStore().powerTable[1].label},
  {type: "primary", data: userStore().powerTable[2].label, msg: userStore().powerTable[2].label},
  {type: "success", data: userStore().powerTable[3].label, msg: userStore().powerTable[3].label},
  {type: "danger", data: userStore().powerTable[4].label, msg: userStore().powerTable[4].label},
]

function goToPortal() {
  window.open(portalUrl, '_blank');
}
</script>

<template>
  <div class="user-base-msg-box">
    <div class="user-avatar-box">
      <el-avatar :size="60" :src="userLogin.imageUrl"/>
    </div>
    <div class="user-info-box">
      <div class="user-other-box">
        <div class="none-box"/>
        <div class="user-name-box">
          {{ userLogin.name }}
        </div>
        <div class="user-power-box">
          <my-tag :data="userLogin.powerName" :mapping="powerTableOption"/>
        </div>
        <div class="user-role-box">
          <!--          {{ userLogin.roleName }}-->
        </div>
        <div class="border-bottom-box"/>
        <div class="user-msg-box">
          <div class="user-msg-item">
            <div class="msg-title">
              余 额
            </div>
            <div class="msg-value">
              {{ userLogin.money === null ? "0.00" : userLogin.money }}
            </div>
          </div>
          <div class="user-msg-item">
            <div class="msg-title">
              累计消费
            </div>
            <div class="msg-value">
              {{ userLogin.amount === null ? "0.00" : userLogin.amount }}
            </div>
          </div>
        </div>
        <div class="border-bottom-box"/>
        <div class="user-btn-box">
          <div class="user-btn-item">
            个人信息
          </div>
          <div class="user-btn-item" @click="goToPortal()">
            网站首页
          </div>
          <div class="user-btn-item" @click="logout()">
            退出账号
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.user-base-msg-box {
  display: flex;
  position: relative;
  align-items: center;
  flex-direction: column;
  margin: .5rem 0;
  border-radius: 50%;

  .user-avatar-box {
    transition: all 0.8s ease; /* 设置过渡效果 */
    z-index: 99 !important;
  }

  .user-info-box {
    position: absolute;
    top: 90%;
    display: flex;
    opacity: 0;
    visibility: hidden; /* 隐藏元素 */
    align-items: center;
    flex-direction: column;
    cursor: unset;
    transition: all 0.5s ease; /* 设置过渡效果 */
    z-index: 9 !important;

    .user-other-box {
      background-color: #ffffff;
      margin-top: .5rem;
      width: 30rem;
      border-radius: .5rem;

      .user-triangle-box {
        height: 1rem;
        width: 1rem;
      }

      .none-box {
        height: 3rem;
      }

      .user-name-box,
      .user-role-box,
      .user-power-box {
        text-align: center;
        padding: .5rem;
        font-size: 1.7rem;
        font-weight: bolder;
      }

      .border-bottom-box {
        border-bottom: .1rem solid #e8e8ed;
      }

      .user-msg-box {
        display: flex;
        align-items: center;
        justify-content: space-around;
        padding: 1rem;

        .user-msg-item {
          display: flex;
          flex-direction: column;
          align-items: center;

          .msg-title {
            color: #999aaa;
          }

          .msg-value {
            font-size: 1.8rem;
            font-weight: bolder;
          }
        }
      }

      .user-btn-box {
        display: flex;
        align-items: center;
        flex-direction: column;

        .user-btn-item {
          display: flex;
          align-items: center;
          flex-direction: column;
          width: 100%;
          pointer-events: auto;
          cursor: pointer;
          margin: .5rem 0;
          padding: .5rem;
          height: 3.5rem;
        }

        .user-btn-item:hover {
          background-color: #f0f0f5;
        }
      }
    }
  }
}

.user-base-msg-box:hover .user-info-box {
  opacity: 1;
  visibility: visible; /* 可见 */
}

.user-base-msg-box:hover .user-avatar-box {
  transform: translateY(3rem) scale(1.2);
}
</style>